"""自动存储帖子 API Router."""
from typing_extensions import Annotated, List

from fastapi import  APIRouter, Path, Depends

from core.models import Member
from api.v1.dependencies.member import get_current_member
from api.v1.models.ajax import (
    AutoSaveModel, ResponseAutoSaveModel,
    ResponseAutoSaveCountModel, ResponseAutoSaveDeleteModel,
)
from service.ajax import AJAXService

router = APIRouter()


@router.get("/autosaves",
            summary="自动存储列表",
            responses={**AJAXService.responses}
            )
async def autosave_list(
    member: Annotated[Member, Depends(get_current_member)],
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
) -> List[ResponseAutoSaveModel]:
    """返回自动保存列表。"""

    return service.get_autosave_list(member)


@router.get("/autosaves/count",
            summary="自动存储",
            responses={**AJAXService.responses}
            )
async def autosave_count(
    member: Annotated[Member, Depends(get_current_member)],
    service: Annotated[AJAXService, Depends(AJAXService.async_init)]
) -> ResponseAutoSaveCountModel:
    """返回自动存储的数量."""

    return {"count": service.get_autosave_count(member.mb_id)}


@router.get("/autosaves/{as_id}",
            summary="自动保存",
            responses={**AJAXService.responses}
            )
async def autosave_load(
    member: Annotated[Member, Depends(get_current_member)],
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    as_id: int = Path(..., title="自动存储 ID", description="自动存储 ID")
) -> ResponseAutoSaveModel:
    """自动保存内容."""

    return service.get_autosave_content(as_id, member)


@router.post("/autosaves",
             summary="自动存储",
             responses={**AJAXService.responses}
             )
async def autosave(
    member: Annotated[Member, Depends(get_current_member)],
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    data: AutoSaveModel
) -> ResponseAutoSaveCountModel:
    """
临时保存正在撰写的帖子。
return:自动保存的文章数量

### Request Body
-**as_uid**：自动保存UID
-**as_subject**：自动保存文章标题
-**as_content**：自动保存文章内容
    """
    service.autosave_save(member, data)

    return {"count": service.get_autosave_count(member.mb_id)}


@router.delete("/autosaves/{as_id}",
                summary="删除自动存储",
                responses={**AJAXService.responses}
               )
async def autosave_delete(
    member: Annotated[Member, Depends(get_current_member)],
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    as_id: int = Path(..., title="自动存储 ID", description="自动存储 ID")
) -> ResponseAutoSaveDeleteModel:
    """删除临时存储."""
    service.autosave_delete(as_id, member)

    return {"result": "deleted"}
